<template>
  <div class="information">
    <titleText title="我的钱包"></titleText>
    <div class="wallet_info">
      <div class="info_title">可提现余额(元)</div>
      <div class="info_money">{{ userInfo.money }}</div>
      <div class="info_btn">
        <div class="btn_1" @click="withdrawals">立即提现</div>
        <div class="btn_2" @click="goAccount">提现账户</div>
      </div>
    </div>
    <div class="detail_title">
      <div class="title_left">余额明细</div>
      <div class="title_right">
        <div class="right_date">
          <el-date-picker
            v-model="select_time"
            :clearable="false"
            type="daterange"
            size="small"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="dateChange"
            range-separator=" - "
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <img src="../../../assets/my/16.png" alt="" />
        </div>

        <div class="wallet_tab">
          <div
            class="tab_item"
            :class="item.isActive ? 'active' : ''"
            v-for="(item, index) in tabsList"
            :key="index"
            @click="select(index)"
          >
            {{ item.name }}
          </div>
        </div>
      </div>
    </div>
    <div class="tab_content" v-loading="loading">
      <div class="table_th">
        <div class="th_item th_w_1">时间</div>
        <div class="th_item th_w_2">类型</div>
        <div class="th_item th_w_3">金额</div>
        <div class="th_item th_w_4">账户余额</div>
        <div class="th_item th_w_5">描述</div>
      </div>
      <div class="table_td" v-for="(item, index) in tableData" :key="index">
        <div class="td_item th_w_1">{{ item.create_time }}</div>
        <div class="td_item th_w_2">{{ item.money_type_name }}</div>
        <div class="td_item th_w_3">{{ item.change_money }}</div>
        <div class="td_item th_w_4">{{ item.after_money }}</div>
        <div class="td_item th_w_5">{{ item.remark }}</div>
      </div>
    </div>
    <div class="no_data" v-if="tableData.length == 0 && !loading">
      <img class="no_img" src="../../../assets/default/12.png" alt="" />
    </div>
    <paging
      :total="total"
      :pageSize="pageSize"
      @pageChange="pageChange"
    ></paging>
  </div>
</template>


<script name="myWallet" setup>
import emitter from "@/utils/mitt.js";
import { post661f7a29c8ae2, post661f96cd63f40 } from "@/utils/api.js";
import titleText from "../titleText.vue";
import paging from "@/components/paging.vue";
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
let tabsList = reactive([
  {
    name: "全部",
    isActive: true,
  },
  {
    name: "收入",
    isActive: false,
  },
  {
    name: "支出",
    isActive: false,
  },
]);
let money_type = ref(0);
function select(index) {
  money_type.value = index;
  tabsList.forEach((item, i) => {
    if (index === i) {
      item.isActive = true;
    } else {
      item.isActive = false;
    }
  });
  pageNum.value = 1;
  loading.value = true;
  getList();
}
onMounted(() => {
  getData();
  getList();
  emitter.on("withdrawalSuccess", () => {
    getData();
  });
});
onBeforeUnmount(() =>{
    emitter.off("withdrawalSuccess")
})

let loading = ref(true);
let userInfo = reactive({});
function getData() {
  post661f7a29c8ae2({})
    .then((res) => {
      if (res.code == 1) {
        userInfo = Object.assign(userInfo, res.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
let select_time = ref("");
function dateChange(e) {
  select_time.value = e[0] + "/" + e[1];
  loading.value = true;
  getList();
}
let pageNum = ref(1);
let pageSize = ref(10);
let total = ref(0);
let tableData = reactive([]);
function getList() {
  post661f96cd63f40({
    money_type: money_type.value == 0 ? "" : money_type.value,
    create_time: select_time.value,
    page: pageNum.value,
    list_rows: pageSize.value,
  })
    .then((res) => {
      // console.log(res);
      if (res.code == 1) {
        loading.value = false;
        tableData = res.data.data;
      }
    })
    .catch((err) => {
      console.log(err);
    });
}
// 分页更新
function pageChange(e) {
  pageNum.value = e;
  loading.value = true;
  getList();
}

// 立即提现
function withdrawals() {
  router.push({
    path: "/my/withdrawals",
  });
}
// 提现账户
function goAccount() {
  router.push({
    path: "/my/account",
    query: {
      account_type: "",
    },
  });
}
</script>

<style lang="scss" scoped>
.information {
  padding: 18px 16px;
  background: #fff;
  min-height: 437px;
  .wallet_info {
    // width: 100%;
    height: 92px;
    background: url("../../../assets/my/15.png") no-repeat;
    background-size: 100% 100%;
    padding: 14.5px 16px 0;
    .info_title {
      font-size: 8px;
      color: #fff;
    }
    .info_money {
      margin: 7px 0 5px;
      font-size: 14px;
      color: #fff;
      font-weight: bold;
    }
    .info_btn {
      display: flex;
      align-items: center;
      .btn_1 {
        width: 64px;
        height: 20px;
        border-radius: 10px;
        background: #fff;
        font-size: 8px;
        color: #1f52ae;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        margin-right: 12px;
      }
      .btn_2 {
        width: 64px;
        height: 20px;
        border-radius: 10px;
        background: #1f52ae;
        font-size: 8px;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }
    }
  }
  .detail_title {
    margin: 19.5px 0 15.5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .title_left {
      font-size: 9px;
      font-weight: 500;
    }
    .title_right {
      display: flex;
      justify-content: flex-end;
      .right_date {
        font-size: 8px !important;
        border-radius: 2px;
        border: 1px solid #e5e5e5;
        height: 16px;
        // width: 86px;
        width: 150px;
        position: relative;
        img {
          width: 7px;
          height: 7px;
          position: absolute;
          right: 8px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
      .wallet_tab {
        display: flex;
        align-items: center;
        margin-left: 10px;
        .tab_item {
          width: 36px;
          height: 16px;
          background: #f6f7f9;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 8px;
          color: #666666;
          margin-left: 10px;
          cursor: pointer;
          &.active {
            background: #e8edf7;
            color: #1f52ae;
            font-weight: 500;
          }
        }
      }
    }
  }
  .tab_content {
    margin: 15.5px 0 8px 0;
    width: 546px;
    .table_th {
      background: #f6f7f9;
      border-top: 1px solid #eeeeee;
      border-left: 1px solid #eeeeee;
      border-bottom: 1px solid #eeeeee;
      display: flex;
      .th_item {
        padding: 8.5px 12px;
        // padding: 0 12px;
        border-right: 1px solid #eeeeee;
        font-size: 7px;
        color: #666;
      }
    }
    .table_td {
      border-left: 1px solid #eeeeee;
      border-bottom: 1px solid #eeeeee;
      display: flex;
      .td_item {
        padding: 8.5px 12px;
        border-right: 1px solid #eeeeee;
        font-size: 7px;
        display: flex;
        align-items: center;
      }
    }
    .th_w_1 {
      width: 102px;
    }
    .th_w_2 {
      width: 56px;
    }
    .th_w_3 {
      width: 115px;
    }
    .th_w_4 {
      width: 115px;
    }
    .th_w_5 {
      width: 158px;
    }
    .table_td:hover {
      background: #f6f7f9;
    }
  }
}
</style>